<template>
  <div class="login-box">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="60px"
      class="demo-ruleForm"
    >
      <h2>后台管理系统</h2>
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="ruleForm.password"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
        <el-button class="login-btn" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { ref, reactive, defineComponent, toRefs } from 'vue'
import { loginData } from '../type/login'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { login } from '../request/api'
import { useRouter } from 'vue-router'
export default defineComponent({
  setup() {
    const data = reactive(new loginData())

    const router = useRouter() // -->$router

    // 登录
    const ruleFormRef = ref<FormInstance>()

    // 登录表单规则
    const rules = {
      username: [
        {
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 10,
          message: '用户名长度在3到10之间',
          trigger: 'blur'
        }
      ],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 10,
          message: '密码长度在3到10之间',
          trigger: 'blur'
        }
      ]
    }
    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return
      // 对表单内容进行验证
      //  valid 布尔值
      formEl.validate((valid) => {
        if (valid) {
          if (
            data.ruleForm.username == 'admin' &&
            data.ruleForm.password == '000000'
          ) {
            data.isGO = false
            setTimeout(() => {
              data.isGO = !data.isGO
            }, 3000)
            login(data.ruleForm).then((res) => {
              // 将token进行保存
              localStorage.setItem('token', res.data.token)
              // 跳转首页
              router.push('/')
              ElMessage.success('登陆成功！')
            })
          } else if (data.isGO) {
            data.isGO = false
            setTimeout(() => {
              data.isGO = !data.isGO
            }, 3000)
            ElMessage.error('账号或密码错误！')
          }
        } else {
          return false
        }
      })
    }

    // 重置
    const resetForm = () => {
      data.ruleForm.username = ''
      data.ruleForm.password = ''
      console.log(data.isGO)
    }

    return { ...toRefs(data), rules, ruleFormRef, resetForm, submitForm }
  }
})
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  background-color: #1b1028;
  overflow: hidden;
  h2 {
    text-align: center;
    padding: 0 0 20px 0;
  }
  .demo-ruleForm {
    width: 500px;
    margin: 200px auto;
    background-color: #fff;
    padding: 30px;
    border-radius: 40px;
  }
  .login-btn {
    width: 48%;
  }
}
</style>
